package com.example.project_springboot;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        String accessKeyId = "LTAI5tBrJRUgW54HJqsPoBH4";
        String accessKeySecret = "DWoJbMsGQsmobSwRE03TF9kLFUgT11";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "lzw-bucket";
        String objectName = "f43c90ec6faaf4acad3db01b00e079cd.jpg";
        String filePath= "C:\\Users\\Administrator\\Desktop\\f43c90ec6faaf4acad3db01b00e079cd.jpg";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,
                accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = new
                    FileInputStream(filePath);
             // 创建PutObject请求。
             ossClient.putObject(bucketName, objectName,
                    inputStream);
        } catch (OSSException oe) {
        } catch (ClientException ce) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}